<?php
/**
 * Created by PhpStorm.
 * User: 火柴
 * Date: 2016/5/28
 * GitHub: https://github.com/huaiguoguo
 * Email: 290559038@qq.com
 * Time: 18:44
 */

namespace frontend\controllers;

use yii;
use yii\helpers\Json;
use yii\base\Exception;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\base\InvalidParamException;

use common\helper\Dump;
use common\extend\EController;
use frontend\models\ChangePasswordForm;
use common\models\Region;
use common\models\UserAddress;


class UserController extends EController
{
    public $layout = "user";

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only'  => ['logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['signup'],
                        'allow'   => true,
                        'roles'   => ['?'],
                    ],
                    [
                        'actions' => ['logout'],
                        'allow'   => true,
                        'roles'   => ['@'],
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'logout' => ['get'],
                ],
            ],
        ];
    }


    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error'   => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class'           => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * 我的订单
     * @return string
     */
    public function actionIndex()
    {
        $data = [];

        return $this->render("index", $data);
    }

    //订单详情
    public function actionOrderDetail()
    {
        $data = [];

        return $this->render("order-detail", $data);
    }

    //邀请好友
    public function actionUserMarket()
    {
        $data = [];

        return $this->render("user-market", $data);
    }

    //优惠券
    public function actionUserCoupons()
    {
        $data = [];

        return $this->render("user-coupons", $data);
    }


    //我的地址
    public function actionUserAddress()
    {
        $data        = [];
        $UserAddress = UserAddress::find()->where(['user_id'=>Yii::$app->user->getId()])->all();
        $data['addresslist'] = $UserAddress;

        return $this->render("user-address", $data);
    }


    //编辑地址
    public function actionEditUserAddress()
    {
        $data        = [];
        $id = intval(Yii::$app->request->get('id'));
        $UserAddress = UserAddress::findOne($id);
        if ($UserAddress->load(Yii::$app->request->post()) && $UserAddress->validate()) {
            $UserAddress->country = 1;
            $UserAddress->user_id = Yii::$app->user->getId();
            if($UserAddress->save()){
                return $this->goBack();
            }
        }
        $data['model'] = $UserAddress;

        return $this->render("edit-user-address", $data);
    }


    //添加地址
    public function actionAddUserAddress()
    {
        $data        = [];
        $UserAddress = new UserAddress();

        if ($UserAddress->load(Yii::$app->request->post()) && $UserAddress->validate()) {
            $UserAddress->country = 1;
            $UserAddress->user_id = Yii::$app->user->getId();
            if($UserAddress->save()){
                return $this->goBack();
            }
        }
        $data['model'] = $UserAddress;

        return $this->render("add-user-address", $data);
    }


    //修改密码
    public function actionUserResetPwd()
    {
        $data       = [];
        $changeForm = new ChangePasswordForm();
        if (Yii::$app->request->post()) {
            $ChangeData = Yii::$app->request->post('ChangePasswordForm');
            $Validate   = $changeForm->_user->validatePassword($ChangeData['password']);
            if ($Validate) {
                if ($changeForm->load(Yii::$app->request->post()) && $changeForm->validate($changeForm) && $changeForm->resetPassword()) {
                    Yii::$app->user->logout();

                    return $this->goHome();
                }
            } else {
                $changeForm->addError('password', '原密码错误');
            }
        }
        $data['user'] = $changeForm;

        return $this->render("user-reset-pwd", $data);
    }


    public function actionRegionList()
    {
        if (Yii::$app->request->isPost && Yii::$app->request->isAjax) {
            $id                         = Yii::$app->request->post('id');
            $list                       = Region::find()->where(['parent_id' => $id])->all();
            Yii::$app->response->format = yii\web\Response::FORMAT_JSON;

            $selectOptions = '';
            foreach ($list as $key => $value) {
                $selectOptions .= "<option value='" . $value->region_id . "'>" . $value->region_name . "</option>";
            }

            return Yii::$app->response->data = ['data' => $selectOptions, 'code' => 200, 'msg' => 'success'];
        } else {
            echo ';adfasdf';
        }

    }


}